package com.pda.xinma.ui.backdepot;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.pda.xinma.R;
import com.pda.xinma.adapter.BackDepotAdapter;
import com.pda.xinma.bean.BackDepotListBean;
import com.pda.xinma.bean.BackInfoBean;
import com.pda.xinma.bean.ScanBean;
import com.pda.xinma.bean.ScanResultBean;
import com.pda.xinma.bean.request.QRcodeRequest;
import com.pda.xinma.contract.BackDepotContract;
import com.pda.xinma.dialog.AffirmDialog;
import com.pda.xinma.net.NetUtils;
import com.pda.xinma.presenter.BackDepotPresenter;
import com.pda.xinma.view.ScanButView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.wangshen.base.app.AppActivityKey;
import com.wangshen.base.config.StringKey;
import com.wangshen.base.net.bean.BaseAppEntity;
import com.wangshen.base.ui.mvp.base.ui.BaseMvpFragment;
import com.wangshen.base.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class BackDepotWaitFragment extends BaseMvpFragment<BackDepotPresenter> implements BackDepotContract.View, OnRefreshListener, OnLoadMoreListener {
    @BindView(R.id.rv_content)
    RecyclerView rvContent;
    @BindView(R.id.srl_layout)
    SmartRefreshLayout srlLayout;
    BackDepotAdapter backDepotAdapter;
    @BindView(R.id.sb_scan)
    ScanButView sbScan;
    private int pager = 1;

    @Override
    public void setData(BackInfoBean backInfoBean) {

    }

    @Override
    public void setList(List<BackDepotListBean> list) {
        if (pager == 1){
            backDepotAdapter.setNewInstance(list);
        }else {
            backDepotAdapter.addData(list);
        }
        srlLayout.finishRefresh();
        srlLayout.finishLoadMore();
    }

    @Override
    public void closeSuccess() {
        srlLayout.autoRefresh();
    }

    @Override
    public void showMessage(String msg) {
        super.showMessage(msg);
        srlLayout.finishRefresh();
        srlLayout.finishLoadMore();
    }

    @Override
    public BackDepotPresenter getPresenter() {
        return new BackDepotPresenter();
    }

    @Override
    public int getViewId() {
        return R.layout.fragment_list_scan;
    }

    @Override
    public void initView() {
        sbScan.setVisibility(View.VISIBLE);
        backDepotAdapter = new BackDepotAdapter(R.layout.item_back_depot_list, new ArrayList<>());
        rvContent.setAdapter(backDepotAdapter);
        srlLayout.setEnableRefresh(true);
        srlLayout.setEnableLoadMore(true);
        srlLayout.setOnRefreshListener(this);
        srlLayout.setOnLoadMoreListener(this);

    }

    @Override
    public void initLinsenterner() {
        backDepotAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                if (backDepotAdapter.getData().get(position).getEntryStatus().equals("40")){
                    AffirmDialog affirmDialog=new AffirmDialog(mContext, new AffirmDialog.OnConfrim() {
                        @Override
                        public void onConfrim() {
                            presenter.entryClose(backDepotAdapter.getData().get(position).getEntryNo());
                        }
                    });
                    affirmDialog.show("确认要关闭该单子？");
                }else {
                    Bundle bundle=new Bundle();
                    bundle.putString(StringKey.NO,backDepotAdapter.getData().get(position).getEntryNo());
                    startIntent(AppActivityKey.BACKDEPOTDETAILACTIVITY,bundle);
                }

            }
        });
        backDepotAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                if (view.getId() == R.id.tv_close){
                    AffirmDialog affirmDialog=new AffirmDialog(mContext, new AffirmDialog.OnConfrim() {
                        @Override
                        public void onConfrim() {
                            presenter.entryClose(backDepotAdapter.getData().get(position).getEntryNo());
                        }
                    });
                    affirmDialog.show("确认要关闭该单子？");
                }
            }
        });

    }

    @Override
    public void onResume() {
        super.onResume();
        srlLayout.autoRefresh();
        sbScan.setScanCallBack(new ScanButView.ScanCallBack() {
            @Override
            public void callBack(ScanBean scanBean) {
                QRcodeRequest request = new QRcodeRequest();
                request.setQRCode(scanBean.getCode());
                request.setQRCodeType("1");
                getCode(request);
            }
        });
    }

    @Override
    public void onPause() {
        super.onPause();
        sbScan.setScanCallBack(null);
    }

    public void getCode(QRcodeRequest request) {
        NetUtils.geApi().scanTheQRCodeToVerify(request)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<BaseAppEntity<ScanResultBean>>() {
                    @Override
                    public void accept(BaseAppEntity<ScanResultBean> stringBaseAppEntity) throws Exception {
                        if (!stringBaseAppEntity.getCode().equals("200")) {
                            ToastUtils.showToast(mContext, stringBaseAppEntity.getMessage());
                            return;
                        }
                        if (stringBaseAppEntity.getData().getType().equals("A")) {
                            Bundle bundle=new Bundle();
                            bundle.putString(StringKey.CODE,stringBaseAppEntity.getData().getCode());
                            startIntent(AppActivityKey.BACKDEPOTACTIVITY,bundle);
                        }else {
                            ToastUtils.showToast(mContext, "请扫正确的条形码");
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        ToastUtils.showToast(mContext, throwable.getMessage());
                    }
                });
    }
    @Override
    public void getData() {

    }

    @Override
    public void onRefresh(RefreshLayout refreshLayout) {
        pager = 1;
        presenter.selectBackList("10", pager);
    }

    @Override
    public void onLoadMore(RefreshLayout refreshLayout) {
        pager++;
        presenter.selectBackList("10", pager);
    }
}
